# coding=utf-8
# coding=utf-8
from functools import wraps
from flask import session, request
from setting import UserKey
from utils import restful
from utils.redis import Redis
from setting import Prefixs


def login_required(func):
    @wraps(func)
    def inner(*args, **kwargs):
        # print(request.headers)
        token = request.headers.get("Authorization", default="")
        if Redis.exists(Prefixs.user_token_prefix + token):
            return func(*args, **kwargs)
        else:
            return restful.fail(), 401

    return inner


def with_app_context(func):
    @wraps(func)
    def inner(*args, **kwargs):
        from app import app
        with app.app_context():
            func(*args, **kwargs)

    return inner
